import yaml

from utils import singleton

@singleton
class TranslatorConfig:
    def __init__(self):
        self._config = None

    def initialize(self, args):
        with open(args.config, 'r') as f:
            config = yaml.safe_load(f)
        
        # 通过命令行传入的配置的优先级要高于配置文件
        overridden_values = {
            key: value for key, value in vars(args).items() if key in config and value is not None
        }

        config.update(overridden_values)

        self._config = config

    def __getattr__(self, name):
        if self._config and name in self._config:
            return self._config[name]
        raise AttributeError(f"'TranslatorConfig' object has no attribute '{name}'")